/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package cz.muni.fi.pa165.carpark.dao;

import cz.muni.fi.pa165.carpark.EmployeePosition;
import cz.muni.fi.pa165.carpark.entities.Employee;
import java.util.List;
import javax.persistence.*;
import org.springframework.stereotype.Repository;

/**
 *
 * @author Martin Derka
 */
@Repository
public class EmployeeDAOImpl implements EmployeeDAO {
    
    @PersistenceContext
    private EntityManager em;      

    @Override    
    public void createEmp(Employee employee) {
        if (employee == null) {
            throw new IllegalArgumentException("Employee cannot be null.");
        }
        if (employee.getId() != null) {
            throw new IllegalArgumentException("Employee already has an id. It was already stored in the databse.");
        }
        
        if(employee.getName()==null || employee.getPosition() == null ){
            throw new IllegalArgumentException("Employee name and position cannot be null.");
        }
        
        em.persist(employee);
    }

    @Override
    public Employee retrieveEmp(Long id) {
        if (id == null) {
            throw new IllegalArgumentException("Id cannot be null.");
        }
        if (id <= 0) {
            throw new IllegalArgumentException("Id must be positive.");
        }
        return em.find(Employee.class, id);        
    }

    @Override
    public void updateEmp(Employee employee) {
        if (employee == null) {
            throw new IllegalArgumentException("Employee cannot be null.");
        }
        if (employee.getId() == null) {
            throw new IllegalArgumentException("Employee already does not have an id. It has not been stored in the databse yet.");
        }
        em.merge(employee);
    }

    @Override
    public void deleteEmp(Employee employee) {
        if (employee == null) {
            throw new IllegalArgumentException("Employee cannot be null.");
        }
        if (employee.getId() == null) {
            throw new IllegalArgumentException("Employee already does not have an id. It has not been stored in the databse yet.");
        }
        Employee emp = em.find(Employee.class, employee.getId()); // in order to get managed entity
        em.remove(emp);
    }

    @Override
    public List<Employee> retrieveAll() {
        Query query = em.createQuery("SELECT e FROM Employee e");
        List<Employee> employees = query.getResultList();
        return employees;

    }

    @Override
    public List<Employee> retrieveByCategory(EmployeePosition position) {
        TypedQuery<Employee> query = em.createQuery("SELECT e FROM Employee e WHERE e.position = :position", Employee.class).setParameter("position", position);
        List<Employee> employees = query.getResultList();
        return employees;
    }
    
    /**
     * Setter of the manager factory for testing
     * @param em EntityManagerFactory to be set
     */
    public void setEntityManager(EntityManager em) {
        this.em = em;
    }
}
